package cn.shequren.communityPeople.home.ui.view;

import android.content.Context;
import android.support.annotation.ColorInt;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.load.resource.gif.GifDrawable;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.jz.community.basecomm.glideConfig.GlideApp;

import java.lang.reflect.Field;

import me.majiajie.pagerbottomtabstrip.internal.RoundMessageView;
import me.majiajie.pagerbottomtabstrip.item.BaseTabItem;

public class GlideTabItem extends BaseTabItem {

    private Context mContext;
    private ImageView mIcon;
    private final TextView mTitle;
    private final RoundMessageView mMessages;

    //未选中的imgUrl
    private String tabDefaultImgUrl;
    //选中的imgUrl
    private String tabCheckedImgUrl;

    private int mDefaultTextColor = 0x56000000;
    private int mCheckedTextColor = 0x56000000;

    public GlideTabItem(@NonNull Context context) {
        this(context, null);
        this.mContext = context;
    }

    public GlideTabItem(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
        this.mContext = context;
    }

    public GlideTabItem(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;

        LayoutInflater.from(context).inflate(me.majiajie.pagerbottomtabstrip.R.layout.item_normal, this, true);

        mIcon = (ImageView) findViewById(me.majiajie.pagerbottomtabstrip.R.id.icon);
        mTitle = (TextView) findViewById(me.majiajie.pagerbottomtabstrip.R.id.title);
        mMessages = (RoundMessageView) findViewById(me.majiajie.pagerbottomtabstrip.R.id.messages);
    }

    /**
     * 方便初始化的方法(加载网络图片)
     *
     * @param DefaultImgUrl 默认状态的图标
     * @param CheckedImgUrl 选中状态的图标
     * @param title         标题
     */
    public void initialize(String DefaultImgUrl, String CheckedImgUrl, String title) {
        tabDefaultImgUrl = DefaultImgUrl;
        tabCheckedImgUrl = CheckedImgUrl;
        mTitle.setText(title);
    }

    @Override
    public void setChecked(boolean checked) {
        //网络图片
        if (checked) {
            loadImages(tabCheckedImgUrl);
            mTitle.setTextColor(mCheckedTextColor);
        } else {
            loadImages(tabDefaultImgUrl);
            mTitle.setTextColor(mDefaultTextColor);
        }


    }

    private void loadImages(String images) {
        if (images.contains("gif")) {
            GlideApp.with(mContext)
                    .asGif()
                    .load(images)
                    .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
                    .listener(new RequestListener<GifDrawable>() {
                        @Override
                        public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<GifDrawable> target, boolean isFirstResource) {
                            return false;
                        }

                        @Override
                        public boolean onResourceReady(GifDrawable resource, Object model, Target<GifDrawable> target, DataSource dataSource, boolean isFirstResource) {
                            //进行强转，反射修改 动画的maxLoopCount
                            GifDrawable gifTarget = resource;
                            try {
                                Field privateStringField = GifDrawable.class.getDeclaredField("maxLoopCount");
                                privateStringField.setAccessible(true);
                                try {
                                    privateStringField.set(gifTarget, 1);
                                } catch (IllegalAccessException e) {
                                    e.printStackTrace();
                                }
                            } catch (NoSuchFieldException e) {
                                e.printStackTrace();
                            }
                            return false;
                        }
                    }).into(mIcon);
        } else {
            GlideApp.with(mContext)
                    .load(images)
                    .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
                    .into(mIcon);

        }


    }

    @Override
    public void setMessageNumber(int number) {
        mMessages.setMessageNumber(number);

    }

    @Override
    public void setHasMessage(boolean hasMessage) {
        mMessages.setHasMessage(hasMessage);

    }

    @Override
    public String getTitle() {
        return mTitle.getText().toString();
    }

    public void setTextDefaultColor(@ColorInt int color) {
        mDefaultTextColor = color;
    }

    public void setTextCheckedColor(@ColorInt int color) {
        mCheckedTextColor = color;
    }
}
